<template>
  <div>
    <!-- 头部导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <!-- 表单是model 表格是data  border横向边框 stripe隔行换色-->
      <el-table :data="rolesList" border stripe>
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 循环一级权限 -->
            <el-row
              class="bd-bottom"
              :class="i==0?'bd-top':''"
              v-for="(item,i) in scope.row.children"
              :key="item.id"
            >
              <el-col :span="6">
                <el-tag closable @close="removeTag(scope.row,item.id)">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <el-col :span="18">
                <!-- 二级循环 -->
                <el-row
                  :class="i==0?'':'bd-top'"
                  v-for="(item2,i) in item.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="warning"
                      closable
                      @close="removeTag(scope.row,item2.id)"
                    >{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>

                  <!-- 三级循环 -->
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeTag(scope.row,item3.id)"
                      type="success"
                      v-for="(item3,i) in item2.children"
                      :key="item3.id"
                    >{{ item3.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引行 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="item">
            <!-- 修改按钮 -->
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button 
            @click="delRoles(item.row.id)" 
            size="mini" 
            type="danger" 
            icon="el-icon-delete">
            删除
            </el-button>
            <!-- 分配角色按钮 -->
            <el-button
              @click="showSetRightDialog(item.row)"
              size="mini"
              type="warning"
              icon="el-icon-setting"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限的对话框 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="resetDefKeys">
      <!-- 内容主体部分 -->
      <el-tree
        :default-checked-keys="defKeys"
        show-checkbox
        default-expand-all
        node-key="id"
        :data="rightList"
        :props="treeProps"
        ref="treeRef"
        style="height:400px;overflow:scroll;"
      ></el-tree>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button @click="subSetRights" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      setRightDialogVisible: false,
      rightList: [],
      treeProps: {
        children: "children",
        label: "authName"
      },
      defKeys: [],
      roleId:0,
      
      
    };
  },

  methods: {
    //   获取数据
    async getRoles() {
      var res = await this.axios.get("roles");
      console.log(res);
      if (res.meta.status == 200) {
        this.rolesList = res.data;
      }
    },

    // 根据id删除对应权限
    removeTag(row, rightId) {
      this.$cfm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          var res = await this.axios.delete(
            `roles/${row.id}/rights/${rightId}`
          );
          if (res.meta.status == 200) {
            this.$mg({
              type: "success",
              message: "删除成功!"
            });
            row.children = res.data;
          }
        })
        .catch(() => {
          this.$mg({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 展示分配权限对话框
    async showSetRightDialog(row) {
      var res = await this.axios.get("rights/tree");
      if (res.meta.status == 200) {
        this.rightList = res.data;
        this.setRightDialogVisible = true;
      }
      this.getLeaKeys(row);
      this.roleId=row.id;
    },

    // 通过递归,获取角色下所有三级权限的id,
    // 并保存到defKeys数组中
    getLeaKeys(row) {
      // console.log(row);
      row.children.forEach(item => {
        if (!item.children) {
          return this.defKeys.push(item.id);
        }
        this.getLeaKeys(item);
      });
    },

    // 清空defKeys
    resetDefKeys() {
      this.defKeys = [];
    },

    // 提交新角色
    async subSetRights() {
      this.setRightDialogVisible = false;
      console.log(this.$refs);
      
      var keysAll=[
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ]
      var idStr=keysAll.join(',')
      console.log(idStr);
      var res=await this.axios.post(`roles/${this.roleId}/rights`,{
        rids:idStr
      })
      console.log(res);
      
      // put和put不写paramas
      // get和delete写 
      if(res.meta.status==200){
        this.$mg.success('更新成功')
        this.getRoles();
      }
      
    },

    editRoles(id) {},

    delRoles(id) {
      this.$cfm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          console.log("确认删除");
          var res = await this.axios.delete("roles/" + id);
          if (res.meta.status == 200) {
            this.$mg.success("删除成功");
            this.getRoles();
          } else {
            this.$mg.error(res.meta.msg);
          }
        })
        .catch(() => {
          this.$mg.info("已取消删除");
        });
    }

  },

  created() {
    this.getRoles();
  }
};
</script>

<style lang='less' scoped>
.el-tag {
  margin: 8px;
}
.bd-bottom {
  border-bottom: 1px solid rgb(66, 238, 50);
}
.bd-top {
  border-top: 1px solid rgb(66, 238, 50);
}
.el-row {
  display: flex;
  // text-align: center;
}
</style>
